<?php
/**
 * Created by PhpStorm.
 * User: fei
 * Date: 2016/8/23
 * Time: 17:28
 */

namespace Admin\Controller;
//use Think\Controller ;
use Component\AdminController;


class QiyegaikuangController extends AdminController
{
    //增加企业概况信息
    public function append()
    {

        //获取企业基本信息，在下拉框中显示
        $rinfo = $this->getComInfo();
        $this->assign('rinfo', $rinfo);
        // $this -> display();

        if (IS_POST) {
            $model = M('gq001');
//验证必须选择一个公司，否则不能提交
            $GQ006_ID = I('post.GQ006_ID');
            if (empty($GQ006_ID)) {
                // echo '企业单位详细名称不能为空!' . "";
                echo "<script type='text/javascript'>alert('企业单位详细名称不能为空!数据添加失败！');history.back(-1);</script>";
            } else {
                $data = $model->create();
                $res = $model->add();
                if ($res) {
                    $info = $model->select();
                    $this->assign("info", $info);
                    $this->display("append");
                    echo "<script type='text/javascript'>{alert('数据添加成功');}</script>";
                } else {
//                echo '数据添加失败';
                    echo "<script type='text/javascript'>alert('数据添加失败');history.back(-1);</script>";
                }
                return;
            }
        }
        $this->display();
    }

//取得所有企业的名称信息
    function getComInfo()
    {
        //查询全部企业的信息
        $rrinfo = D("gq006")->select(); //二维数组信息
        //array(1=>'经理',2=>'主管',3=>'总监')
        $rinfo = array();
        foreach ($rrinfo as $k => $v) {
            $rinfo[$v['GQ006_ID']] = $v['QA04'];//array(1=>'经理',2=>'主管')
        }
        return $rinfo;
    }

    //查询当前企业概况
    function showlist_gaikuang($GQ006_ID)
    {
        $model = M('gq001');
        $rinfo = $this->getComInfo();
        $this->assign('rinfo', $rinfo);

        $sql = "select * from gq001 where GQ006_ID=$GQ006_ID";
//print_r($sql);
        $info = $model->query($sql);
        //print_r($info);

        $this->assign('info', $info);
        $this->display();

    }

//查询所有企业概况
    function showlist()
    {

        $model = D("gq001");

        $rinfo = $this->getComInfo();
        $this->assign('rinfo', $rinfo);
        // $this -> display();

        //1. 获得当前记录总条数
        $total = $model->count();
        $per = 7;
        //2. 实例化分页类对象
        $page = new \Component\Page($total, $per); //autoload
        //3. 拼装sql语句获得每页信息
        $sql = "select * from gq001 " . $page->limit;
        $info = $model->query($sql);
        //4. 获得页码列表
        $pagelist = $page->fpage();

        $this->assign('info', $info);
        $this->assign('pagelist', $pagelist);
        $this->display();
    }
//企业填报人修改企业概况
    function upd($GQ001_ID)
    {
        //查询被修改商品的信息并传递给模板展示
        $model = D("gq001");
        //两个逻辑：展示表单、收集表单
        if (IS_POST) {
            $model->create();
            $rst = $model->save();
            if ($rst) {
                echo "<script type='text/javascript'>alert('数据修改成功');history.back(-1);</script>";;
            } else {
                echo "<script type='text/javascript'>alert('数据修改失败');history.back(-1);</script>";
            }
        } else {
            $info = $model->find($GQ001_ID); //一维数组

            $this->assign('info', $info);

            $rinfo = $this->getComInfo();
            $this->assign('rinfo', $rinfo);

            $this->display();

        }
    }

//企业填报人删除企业概况
    function del($GQ001_ID)
    {
        //查询被修改商品的信息并传递给模板展示
        $model = D("gq001");


        $res = $model->delete($GQ001_ID);

        if ($res) {

            //$this->redirect('Qiyegaikuang/showlist', array(), 1, '数据删除成功')


//            $info = $model->select();
//            $this->assign("info", $info);
//            $this->display("showlist");


            echo "<script type='text/javascript'>{alert('数据删除成功');history.back(-1);}</script>";
            //$this->showlist();
            //$this->success('删除成功');

        } else {
            echo "<script type='text/javascript'>alert('数据删除失败');history.back(-1);</script>";
        }
        $this->display;
    }

//是否高新区管委会直属（为了在导出列表中显示）
    function getQB101_1()
    {
        //查询全部企业概况的信息
        $rrinfo = D("gq001")->select(); //二维数组信息
        //array(1=>'经理',2=>'主管',3=>'总监')
        $rinfo_QB101_1 = array();
        foreach ($rrinfo as $k => $v) {
            //$rinfo[$v['GQ006_ID']] = $v['QA04'];//array(1=>'经理',2=>'主管')
            if($v['QB101_1']== 0){
                $rinfo_QB101_1[$v['QB101_1']] = 否;
                              }elseif($v['QB101_1']==1){
                $rinfo_QB101_1[$v['QB101_1']] = 是;
                              }else{
                null;
            }
        }
        return $rinfo_QB101_1;
    }
//是否执行2006《企业会计推测》（QB20）

    function getQB20()
    {
        //查询全部企业概况的信息
        $rrinfo = D("gq001")->select(); //二维数组信息
        //array(1=>'经理',2=>'主管',3=>'总监')
        $rinfo_QB20 = array();
        foreach ($rrinfo as $k => $v) {
            //$rinfo[$v['GQ006_ID']] = $v['QA04'];//array(1=>'经理',2=>'主管')
            if($v['QB20']== 0){
                $rinfo_QB20[$v['QB20']] = 否;
            }elseif($v['QB20']==1){
                $rinfo_QB20[$v['QB20']] = 是;
            }else{
                null;
            }
        }
        return $rinfo_QB20;
    }
    //是否进入国家高新技术产业开发区(QB07）
    function getQB07()
    {
        //查询全部企业概况的信息
        $rrinfo = D("gq001")->select(); //二维数组信息
        //array(1=>'经理',2=>'主管',3=>'总监')
        $rinfo_QB07 = array();
        foreach ($rrinfo as $k => $v) {
            //$rinfo[$v['GQ006_ID']] = $v['QA04'];//array(1=>'经理',2=>'主管')
            if($v['QB07']== 0){
                $rinfo_QB07[$v['QB07']] = 否;
            }elseif($v['QB07']==1){
                $rinfo_QB07[$v['QB07']] = 是;
            }else{
                null;
            }
        }
        return $rinfo_QB07;
    }
//是否经过认证或复审为高新企业(QB11）
    function getQB11()
    {
        //查询全部企业概况的信息
        $rrinfo = D("gq001")->select(); //二维数组信息
        //array(1=>'经理',2=>'主管',3=>'总监')
        $rinfo_QB11 = array();
        foreach ($rrinfo as $k => $v) {
            //$rinfo[$v['GQ006_ID']] = $v['QA04'];//array(1=>'经理',2=>'主管')
            if($v['QB11']== 0){
                $rinfo_QB11[$v['QB11']] = 否;
            }elseif($v['QB11']==1){
                $rinfo_QB11[$v['QB11']] = 是;
            }else{
                null;
            }
        }
        return $rinfo_QB11;
    }
//认定或复审（QB19）
    function getQB19()
    {
        //查询全部企业概况的信息
        $rrinfo = D("gq001")->select(); //二维数组信息
        //array(1=>'经理',2=>'主管',3=>'总监')
        $rinfo_QB19 = array();
        foreach ($rrinfo as $k => $v) {
            //$rinfo[$v['GQ006_ID']] = $v['QA04'];//array(1=>'经理',2=>'主管')
            if($v['QB19']== 0){
                $rinfo_QB19[$v['QB19']] = 否;
            }elseif($v['QB19']==1){
                $rinfo_QB19[$v['QB19']] = 是;
            }else{
                null;
            }
        }
        return $rinfo_QB19;
    }



    //导出企业概况表格
    public function output()
    {
//获取所需数据
        $res = M('gq001')->select();

        $rinfo = $this->getComInfo();
        $this->assign('rinfo', $rinfo);

        $rinfo_QB101_1 = $this->getQB101_1();
        $this->assign('rinfo_QB101_1', $rinfo_QB101_1);

        $rinfo_QB20 = $this->getQB20();
        $this->assign('rinfo_QB20', $rinfo_QB20);

        $rinfo_QB07 = $this->getQB07();
        $this->assign('rinfo_QB07', $rinfo_QB07);

        $rinfo_QB11 = $this->getQB11();
        $this->assign('rinfo_QB11', $rinfo_QB11);

        $rinfo_QB19 = $this->getQB19();
        $this->assign('rinfo_QB19', $rinfo_QB19);


//        $export = I('export');//触发导出指令的值
//        if ($export ==１){
        if (IS_POST) {


            $xlsName = "企业概况列表";//表名
            $xlsCell = array(
                         array('QA04', '企业单位详细名称'),
                         array('QB101', '企业隶属关系') ,
                         array('QB101_1', '是否高新区管委会直属') ,
                         array('QB02', '行业类别'),
                array('QB03', '行业代码'),
                array('QB04', '企业注册时间'),
                array('QB04_0', '注册资金(QB04_0)（千元）'),
                array('QB20', '是否执行2006《企业会计推测》（QB20）'),
                array('QB06', '企业等级注册类型（QB06）'),

                array('QB07', '是否进入国家高新技术产业开发区(QB07）'),
                array('QB08', '进区时间（QB08）'),
                array('QB09', '企业类型(QB09)'),
                array('QB10', '上级法人单位机构代码(QB10)'),
                array('QB11', '是否经过认证或复审为高新企业(QB11）'),

                array('QB12', '企业被认定(复审)为高新技术企业时间（QB12）'),
                array('QB19', '认定或复审（QB19）'),
                array('QB13', '认定或复审企业编号(QB13)'),
                array('QB14', '与科技企业孵化器关系（QB14）'),
                array('QB14_1', '入孵时间（QB14_1）'),

                array('QB14_2', '毕业时间（QB14_2）'),
                array('QB15', '上市及及三板、四板挂牌情况(QB15)'),
                array('QB15_1', '股票编号(QB15_1)'),
                array('QB15_2', '上市挂牌时间（QB15_2）'),
                array('QB18', '控股情况(QB18)'),



            );
                    $xlsData = array();
                      foreach ($res as $key => $val) {
                          array_push($xlsData, array(//这里的需要导出的内容，要注意键名跟上面的字段键名要一致
                             // 'QA04' => $val[GQ006_ID],
                              'QA04' => $rinfo[$val[GQ006_ID]],


//                              'QB101' => date('Y-m-d H:i:s', $val[createtime]),
                              'QB101' => $val[QB101],

                                'QB101_1' => $rinfo_QB101_1[$val[QB101_1]],
                              'QB02' => $val[QB02],

                              'QB03' => $val[QB03],
                              'QB04' => $val[QB04],
                              'QB04_0' => $val[QB04_0],
                              'QB20' => $rinfo_QB20[$val[QB20]],
                              'QB06' => $val[QB06],

                              'QB07' => $rinfo_QB07[$val[QB07]],
                              'QB08' => $val[QB08],
                              'QB09' => $val[QB09],
                              'QB10' => $val[QB10],
                              'QB11' => $rinfo_QB11[$val[QB11]],

                              'QB12' => $val[QB12],
                              'QB19' => $rinfo_QB19[$val[QB19]],
                              'QB13' => $val[QB13],
                              'QB14' => $val[QB14],
                              'QB14_1' => $val[QB14_1],

                              'QB14_2' => $val[QB14_2],
                              'QB15' => $val[QB15],
                              'QB15_1' => $val[QB02],
                              'QB15_2' => $val[QB15_2],
                              'QB18' => $val[QB18],


                          ));
                      }

                exportExcel($xlsName, $xlsCell, $xlsData);//这里就是调用写在function里的函数了。
 }
        die();
    }


}

    